package cn.wolfcode.luowowo.website.web.controller;

import cn.wolfcode.luowowo.cache.service.IUserInfoRedisService;
import cn.wolfcode.luowowo.cache.util.RedisKeys;
import cn.wolfcode.luowowo.common.exception.LogicException;
import cn.wolfcode.luowowo.common.util.AjaxResult;
import cn.wolfcode.luowowo.member.domain.UserInfo;
import cn.wolfcode.luowowo.member.service.IUserInfoService;
import cn.wolfcode.luowowo.website.util.CookieUtil;
import com.alibaba.dubbo.config.annotation.Reference;
import org.springframework.boot.web.servlet.server.Session;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletResponse;
import java.util.UUID;

@Controller
public class LoginController {

    @Reference
    private IUserInfoService userInfoService;

    @Reference
    private IUserInfoRedisService userInfoRedisService;


    @RequestMapping("/hello")
    public Object get(Long id){
        return userInfoService.get(id);
    }

    /**
     * 手机号码的唯一校验
     * @param phone
     * @return
     */
    @RequestMapping("/checkPhone")
    @ResponseBody
    public boolean checkPhone(String phone){
        return !userInfoService.checkPhone(phone);
    }

    /**
     * 发送验证码
     * @param phone
     * @return
     */
    @RequestMapping("/sendVerifyCode")
    @ResponseBody
    public Object sendVerifyCode(String phone){
        userInfoService.sendVerifyCode(phone);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userRegist")
    @ResponseBody
    public Object userRegist(String phone,String nickname,String password,String rpassword,String verifyCode){
        userInfoService.userRegist(phone,nickname,password,rpassword,verifyCode);
        return AjaxResult.SUCCESS;
    }

    @RequestMapping("/userLogin")
    @ResponseBody
    public Object userLogin(String username, String password, HttpServletResponse resp){
        String token = userInfoService.userLogin(username,password);

        /*//4.创建一个Cookie,将token设置到cookie后通过response对象带到浏览器.并且设置过时时间\
        Cookie cookie = new Cookie("token",token);
        //4.1设置共享路径
        cookie.setPath("/");
        //4.2设置时效
        cookie.setMaxAge(RedisKeys.USER_LOGIN.getTime().intValue());
        resp.addCookie(cookie);*/
        CookieUtil.addCookie(resp,"token",token,RedisKeys.USER_LOGIN.getTime().intValue());
        return AjaxResult.SUCCESS;
    }
}
